package cn.xopencode.scoreservice.service.demand.impl;

import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.mybatis.core.util.PageUtil;
import cn.xopencode.score.api.rpc.demand.dto.ScoreDetailsRespDTO;
import cn.xopencode.score.api.rpc.demand.dto.ScoreReqDTO;
import cn.xopencode.score.api.rpc.demand.dto.ScoreRespDTO;
import cn.xopencode.scoreservice.convert.demand.ScoreConvert;
import cn.xopencode.scoreservice.convert.demand.ScoreDetailsConvert;
import cn.xopencode.scoreservice.dal.mysql.dataobject.demand.ScoreDO;
import cn.xopencode.scoreservice.dal.mysql.mapper.demand.ScoreMapper;
import cn.xopencode.scoreservice.service.demand.ScoreService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 积分情况 Service 实现
 */
@Service
@Validated
public class ScoreServiceImpl implements ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public List<ScoreRespDTO> pageScore() {
        System.out.println("service impl...");
        List<ScoreDO> scoreDOIPage =
//                scoreMapper.selectAllPage();
                new ArrayList<>();
        ScoreDO sd1 = new ScoreDO();
        sd1.setEmpId("1001");
        sd1.setScore(10.0);
        sd1.setSuggestScore(2.0);
        sd1.setUpdateTime(new Date());
        sd1.setAccidentScore(1.0);
        sd1.setCheckScore(1.0);
        sd1.setCreatorId("1004");
        sd1.setActivityScore(-3.0);
        sd1.setTrainingScore(9.0);
        sd1.setId("1");
        sd1.setRemark("测试1");
        sd1.setCreateTime(new Date());
        sd1.setName("张三");

        ScoreDO sd2 = new ScoreDO();
        sd2.setEmpId("1002");
        sd2.setName("李四");
        sd2.setScore(10.0);
        sd2.setSuggestScore(2.0);
        sd2.setUpdateTime(new Date());
        sd2.setAccidentScore(1.0);
        sd2.setCheckScore(1.0);
        sd2.setCreatorId("1004");
        sd2.setActivityScore(-3.0);
        sd2.setTrainingScore(9.0);
        sd2.setId("2");
        sd2.setRemark("测试1");
        sd2.setCreateTime(new Date());

        ScoreDO sd3 = new ScoreDO();
        sd3.setEmpId("1003");
        sd3.setName("王五");
        sd3.setScore(10.0);
        sd3.setSuggestScore(2.0);
        sd3.setUpdateTime(new Date());
        sd3.setAccidentScore(1.0);
        sd3.setCheckScore(1.0);
        sd3.setCreatorId("1004");
        sd3.setActivityScore(-3.0);
        sd3.setTrainingScore(9.0);
        sd3.setId("3");
        sd3.setRemark("测试3");
        sd3.setCreateTime(new Date());

        scoreDOIPage.add(sd1);
        scoreDOIPage.add(sd2);
        scoreDOIPage.add(sd3);

        System.out.println(scoreDOIPage);
//        return ScoreConvert.INSTANCE.convertPage(scoreDOIPage);
        return ScoreConvert.INSTANCE.convertList(scoreDOIPage);
    }

    @Override
    public PageResult<ScoreRespDTO> pageDateScore(ScoreReqDTO scoreReqDTO) {
        IPage<ScoreDO> scoreDOIPage = scoreMapper.selectDatePage(scoreReqDTO);
        return ScoreConvert.INSTANCE.convertPage(scoreDOIPage);
    }

    @Override  //mybatis-plus  通用mapper
    public ScoreDetailsRespDTO getDetailsScore(String empId) {
        ScoreDO scoreDO = scoreMapper.selectByEmpId(empId);

        return ScoreDetailsConvert.INSTANCE.convert(scoreDO);
    }

    @Override
    public Double addSuggestScore(String empId,Double suggestScore) {
        ScoreDO scoreDO = scoreMapper.selectByEmpId(empId);
        scoreDO.setSuggestScore(scoreDO.getSuggestScore()+suggestScore);
        scoreDO.setScore(scoreDO.getScore()+suggestScore);
        scoreDO.setEmpId(empId);
        scoreMapper.updateSuggestScore(scoreDO);
        return suggestScore;
    }
}
